import axios from 'axios';
import { message } from 'antd';
import {baseUrl} from '../config';
import * as utils from '../utils/action';
const $axios = axios.create({
	baseURL: process.env.REACT_APP_BASE_URL,
	timeout: 60000,
	retry:4,
	retryDelay:1000
});

//请求拦截
$axios.interceptors.request.use(
	function(config) {
		// 在发送请求之前做些什么
		// 通过reudx的store拿到拿到全局状态树的token ，添加到请求报文，后台会根据该报文返回status
		// 此处应根据具体业务写token
		// const token = store.getState().user.token || localStorage.getItem('token');
		// const token = 'auth_76296635-05b6-4cd3-983b-58851d3f6045';
		// config.headers['Token'] = token;
		const token = localStorage.getItem('token');
		const Userid = localStorage.getItem('userId');
		config.headers['Token'] = token===undefined?'':token;
		config.headers['Userid'] = Userid === undefined?'':Userid;
		config.headers['roleCode']=2;
		config.baseURL=baseUrl;
		//config.baseURL='http://192.168.31.153:9003';
		return config;
	},
	
	function(error) {
		// 对请求错误做些什么
		message.error(error);
		return Promise.reject(error);
	}
);

// 添加响应拦截器
$axios.interceptors.response.use(
	function(response) {
		// 对响应数据做点什么
		if (response.data.success === false) {
			message.error(response.data.message);
		}
		return response.data;
	},
	function(error) {
		if(error.response!==undefined){
			if(error.response.status===401){
				// utils.openNotification('error','用户token过期,请重新登录!');
				window.location.href="#/Login"
			}else if(error.response.status===408){
				window.location.href="#/Login"
			}
			else if(error.response.status===500){
				utils.openNotification('error','服务器错误！');
			}
			else if(error.response.status===404){
				utils.openNotification('error','内部错误');
			}
			else if(error.response.status===400){
				utils.openNotification('error','请求错误');
			}
			else if(error.response.status===504){
				utils.openNotification('error','连接超时');
			}
		}else{
			utils.openNotification('error','网络连接错误！请检查网络');
		}
		if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
			var config = error.config;
			config.__retryCount = config.__retryCount || 0;
			if (config.__retryCount >= config.retry) {
				
				// Reject with the error
				//window.location.reload();
				return Promise.reject(error);
			}

			// Increase the retry count
			config.__retryCount += 1;

			// Create new promise to handle exponential backoff
			var backoff = new Promise(function(resolve) {
				setTimeout(function() {
					
					//console.log('resolve');
					resolve();
				}, config.retryDelay || 1);
			});

			return backoff.then(function() {
				return axios(config);
			});
		} else {
			return Promise.reject(error);
		}
	}
);

export default $axios;
